﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace InternetChecksum
{
    public partial class Form1 : Form
    {
        private string result = "", overflow = "0", carries = " ";

        public Form1()
        {
            InitializeComponent();
        }

         private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void CalcButton_Click(object sender, EventArgs e)
        {
            result = "";
            overflow = "0"; 
            carries = " ";
            BinAdd(InputBox1.Text, InputBox2.Text);
            OutputBox.Text = result;
            CarryBox.Text = carries;
            OverflowBox.Text = overflow;
            for (int i = 0; i < InputBox1.Text.Length - 1; i++)
            {
                overflow = "0" + overflow;
            }
            BinAdd(result, overflow);
            WrapAroundBox.Text = result;
            carries = "";
            for (int i = 0; i < result.Length; i++)
            {
                carries += Complement(result[i]);
            }
            ComplementBox.Text = carries;
        }

        private int C2I(char c)
        {
            if (c == '1')
            {
                return 1;
            }
            else
            {
                return 0;
            }
        }

        private void BinAdd(string input1, string input2)
        {
            if (input1.Length != input2.Length)
            {
                MessageBox.Show("Missmatched lengths", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                result = "";
                carries = " ";
                for (int i = (input1.Length - 1); i >= 0; i--)
                {
                    int carry = 0, sum = 0;
                    if (carries[0] == '1')
                    {
                        carry = 1;
                    }
                    sum = C2I(input1[i]) + C2I(input2[i]) + carry;
                    result = (sum % 2).ToString() + result;
                    if (sum > 1)
                    {
                        if (i > 0)
                        {
                            carries = "1" + carries;
                        }
                        else
                        {
                            overflow = "1";
                        }
                    }
                    else
                    {
                        carries = ' ' + carries;
                    }
                }
            }
        }

        private char Complement(char c)
        {
            if (c == '1')
            {
                return '0';
            }
            else
            {
                return '1';
            }
        }
    }
}
